package com.pinganfu.mock.server;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;






import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONObject;
import org.json.XML;

import com.pinganfu.mock.test.ObjectServerApp;

public class server extends Thread {
	
	private Socket client;  
	  
    /* 
     * 构造函数，接收每一个socket实例 
     */  
    public server(Socket socket) {  
        this.client = socket;  
    } 
    
    
	
	public void run(){
		try {
        	System.out.println("=============================");

			System.out.println("线程开启");  
			
			//对象数据流的获取与返回  
			 ObjectInputStream in = new ObjectInputStream(client.getInputStream());
			 ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream()); 
	         
	         Object jsonString  = in.readObject();
	         System.out.println("服务器端得到数据：" + jsonString.toString());
	         String jsString = "1asd";
	         System.out.println("=============================");
	         System.out.println(jsString);
	         
	         out.writeObject(jsString);  
	         out.flush();
	         in.close();  
	         out.close();
	         
		} catch (Exception e) {  
        } finally {  
        } 
         
	}
	
	public void st() throws IOException{
		 // 创建服务端，监听端口6688  
        ServerSocket server = new ServerSocket(8081);  
        System.out.println("服务器已经开启，等待连接。");  
        // 使用服务器端保持永久监听状态  
        while (true) {  
            // 接收每一个客户端的连接，并返回socket实例  
            ObjectServerApp osa = new ObjectServerApp(server.accept());  
            // 为每一个客户端启一个线程，去执行操作  
            osa.run();  
        }  
	}
	
	
	/*public static void main(String[] args) throws IOException, ClassNotFoundException{
		BufferedWriter buffWri=null;  
		
		//创建服务器端的Socket，并监听端口8888  
        ServerSocket socketConnection = new ServerSocket(8081);  
        
        System.out.println("服务器已经开启，等待连接。");  
        System.out.println("");
        while (true) {
        String ssString ="";
        //接收客户端连接，并返回一个socket对象  
        Socket scoket = socketConnection.accept();  
        
        //对象数据的输入与输出，需要用ObjectInputStream和ObjectOutputStream进行  
        ObjectInputStream in = new ObjectInputStream(scoket.getInputStream());  
        ObjectOutputStream out = new ObjectOutputStream(scoket.getOutputStream());
        
        String jsonString  = (String) in.readObject();
        String json2  = (String) in.readObject();
        System.out.println("测试1"+jsonString);
        System.out.println("测试2"+json2);
        
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String request_time = df.format(new Date());
		
        System.out.println(request_time +"\t服务器端得到数据：" + jsonString.toString());
        
        String fileName2="json.xml";    
        buffWri=new BufferedWriter(new FileWriter(fileName2));    
        JSONObject jsonObj=new JSONObject(jsonString);    
        buffWri.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        buffWri.write("<ROOT>");
        buffWri.write(XML.toString(jsonObj));
        buffWri.write("</ROOT>");    
        buffWri.flush();    
        
        final RandomAccessFile randomFile = new RandomAccessFile(fileName2,"rw");
        String tmp = "";     
        while( (tmp = randomFile.readLine())!= null) {     
        	ssString =ssString+"\n"+ new String(tmp.getBytes("ISO8859-1"));     
        }     
        
        out.writeObject(ssString);  
        out.flush();
        
        in.close();  
        out.close();
        }
        
	}*/
	
	
	
	
	public static void main(String[] args) throws IOException {
		 // 创建服务端，监听端口6688  
        ServerSocket server = new ServerSocket(6688);  
        System.out.println("服务器已经开启，等待连接。");  
        // 使用服务器端保持永久监听状态  
        while (true) {  
            // 接收每一个客户端的连接，并返回socket实例  
            ObjectServerApp osa = new ObjectServerApp(server.accept());  
            // 为每一个客户端启一个线程，去执行操作  
            osa.run();  
        }  
	}
}
